<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\AliPayAppController;
use App\Http\Controllers\UploadController;
use App\Http\Controllers\AliPayController;
use App\Http\Controllers\PaymentController;
use App\Http\Validators\UserVideosReport;
use App\Http\Validators\ValidatorFactory;
use App\Models\BrowseJob;
use App\Models\CollArea;
use App\Models\CollPosition;
use App\Models\CompanyBasic;
use App\Models\Levels;
use App\Models\ProductOrder;
use App\Models\Resume;
use App\Models\User;
use App\Models\UserAttentions;
use App\Models\UserVideoBroswer;
use App\Models\UserVideoCollect;
use App\Models\UserVideoPraise;
use App\Models\UserVideos;
use App\Models\VipOrder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Validator, DB, Redis;

class VideosController extends CommonController
{
    /**
     * 我的主页统计
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function videoCount(Request $request)
    {
        $uid = $request->uid;

        $tuid = $request->tuid;
        if (!$tuid) {
            $tuid = $uid;
        }

        $info = User::select('uid', 'name', 'avatar')->where('uid', $tuid)->first();

        $num1 = UserAttentions::where('fuid', $tuid)->count();//我的关注
        $num2 = UserAttentions::where('tuid', $tuid)->count();//我的粉丝

//        $num3 = 0;
//        $num4 = 0;
//        $videoIds = UserVideos::where('uid', $tuid)->pluck('id');
//        if ($videoIds) {
//            $num3 = UserVideoPraise::whereIn('video_id', $videoIds)->count();
//            $num4 = UserVideoCollect::whereIn('video_id', $videoIds)->count();
//        }
        //我的点赞和收藏
        $num3 = DB::table('user_video_praise')
            ->join('user_videos', 'user_video_praise.video_id', '=', 'user_videos.id')
            ->where('user_video_praise.uid', $uid)
            ->count();
        $num4 = DB::table('user_video_collect')
            ->join('user_videos', 'user_video_collect.video_id', '=', 'user_videos.id')
            ->where('user_video_collect.uid', $uid)
            ->count();
//        Log::debug($num3);
//        Log::debug($num4);
        $info->total1 = $num1;
        $info->total2 = $num2;
        $info->total3 = $num3 + $num4;

        $isAttention = UserAttentions::where('fuid', $uid)->where('tuid', $tuid)->first();
        $info->is_attention = $isAttention ? 1 : 0;

        return response()->json(['status' => 1, 'data' => $info]);
    }

    public function videoList(Request $request)
    {
        $totalList = 10;

        $uid = $request->uid;
        $page = $request->page;
        $type = $request->type;

        $start = ($page - 1) * $totalList;
        $videos = [];
        if ($type == 1) {
            $attentions = UserAttentions::select('tuid')
                ->where('fuid', $uid)
                ->get();

            if ($attentions) {
                foreach ($attentions as $v1) {
                    $video = UserVideos::where('uid', $v1->tuid)
                        ->orderBy('created_at', 'desc')
                        ->get()->toArray();
                    $videos = array_merge($videos, $video);
                }
            }
        } elseif ($type == 2) {
            Redis::select(3);
            $idsArr = Redis::zRevRange('resources_praise', 0, -1);
//            Log::channel('test')->info(json_encode($idsArr));
            if ($idsArr) {
                foreach ($idsArr as $v2) {
                    $video = UserVideos::where('id', $v2)
                        ->first();
                    if ($video['uid'] == $uid) {
                        continue;
                    }
                    $videoStr = json_encode($video);
                    $videoArr = json_decode($videoStr, true);
                    $videos[] = $videoArr;
                }
            }

        }

        if ($videos) {
            foreach ($videos as $k3 => $v3) {
                if ($v3['status'] != 1) {
                    unset($videos[$k3]);
                    continue;
                }
//                $broswer = UserVideoBroswer::where('uid', $uid)
//                    ->where('cate', $cate)
//                    ->where('video_id', $v3['id'])
//                    ->first();
//                if ($broswer) {
//                    unset($videos[$k3]);
//                }
            }
        }

        $videos = array_slice(array_values(array_filter($videos)), $start, $totalList);

        if ($videos) {
            foreach ($videos as &$v) {
                $v['srcs'] = explode(',', $v['srcs']);

                $avatar = DB::table('users')->where('uid', $v['uid'])->value('avatar');
                $name = DB::table('users')->where('uid', $v['uid'])->value('name');
                $v['avatar'] = $avatar;
                $v['name'] = $name;
                $v['praise_num'] = UserVideoPraise::where('video_id', $v['id'])->count();
                $v['collect_num'] = UserVideoCollect::where('video_id', $v['id'])->count();

                $is_attention = UserAttentions::where('fuid', $uid)->where('tuid', $v['uid'])->value('id');
                $is_praise = UserVideoPraise::where('uid', $uid)->where('video_id', $v['id'])->value('id');
                $is_collect = UserVideoCollect::where('uid', $uid)->where('video_id', $v['id'])->value('id');

                $v['is_attention'] = $is_attention > 0 ? 1 : 0;
                $v['is_praise'] = $is_praise > 0 ? 1 : 0;
                $v['is_collect'] = $is_collect > 0 ? 1 : 0;
            }
        }

        return response()->json(['status' => 1, 'data' => $videos]);
    }

    public function otherVideoList(Request $request)
    {
        $totalList = 10;

        $uid = $request->uid;
        $index = $request->index;
        $page = $request->page;
        $tuid = $request->tuid;

        $start = $index + ($page - 1) * $totalList;

        $videos = UserVideos::where('uid', $tuid)
            ->orderBy('created_at', 'desc')
            ->skip($start)
            ->take($totalList)
            ->get()->toArray();

        Log::debug($videos);

        if ($videos) {
            foreach ($videos as &$v) {
                $v['srcs'] = explode(',', $v['srcs']);

                $table = 'users';
                $field1 = 'avatar';
                $field2 = 'name';

                $avatar = DB::table($table)->where('uid', $v['uid'])->value($field1);
                $name = DB::table($table)->where('uid', $v['uid'])->value($field2);
                $v['avatar'] = $avatar;
                $v['name'] = $name;
                $v['praise_num'] = UserVideoPraise::where('video_id', $v['id'])->count();
                $v['collect_num'] = UserVideoCollect::where('video_id', $v['id'])->count();

                $is_attention = UserAttentions::where('fuid', $uid)->where('tuid', $v['uid'])->value('id');
                $is_praise = UserVideoPraise::where('uid', $uid)->where('video_id', $v['id'])->value('id');
                $is_collect = UserVideoCollect::where('uid', $uid)->where('video_id', $v['id'])->value('id');

                $v['is_attention'] = $is_attention > 0 ? 1 : 0;
                $v['is_praise'] = $is_praise > 0 ? 1 : 0;
                $v['is_collect'] = $is_collect > 0 ? 1 : 0;
            }
        }

        return response()->json(['status' => 1, 'data' => $videos]);
    }

    /**
     * 发布资源
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addVideo(Request $request)
    {
        $input = $request->input();

        $rules = [
            'type' => 'required',
            'data' => 'required'
        ];

        $messages = [
            'type.required' => '资源类型缺失',
            'data.required' => '请上传资源',
        ];

        $validate = Validator::make($input, $rules, $messages);
        if ($validate->fails()) {
            return response()->json(['status' => 0, 'msg' => $validate->errors()->first()]);
        }

        $dataArr = json_decode($input['data'], true);

        $srcArr = [];
        foreach ($dataArr as $v) {
            if (is_array($v)) {
                array_merge($srcArr, $v);
            } else {
                array_push($srcArr, $v);
            }
        }

        $srcStr = implode(',', $srcArr);

        $type = $input['type'];
        $coverImg = $input['cover_img'];
        if (!$coverImg) {
            if ($type == 1) {
                $coverImg = reset($srcArr);
            } elseif ($type == 2) {
                $name = create_order($input['uid']);
                $file = '/www/wwwroot/wine.hnliandesy.com/wine/public/video/' . $name . '.png';
                $str = "/ffmpeg/ffmpeg -i $srcStr -y -f mjpeg -ss 3 -t 1 -s 135x182 $file";//必须是命令的完整路径
                exec($str, $output, $return_val);
                if ($return_val != 0) {
                    return response()->json(['status' => 0, 'msg' => '生成封面图失败，请手动上传封面图！']);
                }

                $dstFile = 'pictures/' . $name . '.png';
                $upload = new UploadController();
                $res = $upload->uploadImageCommon($dstFile, $file);

                if ($res['http_code'] != 200) {
                    return response()->json(['status' => 0, 'msg' => '封面图上传失败，请手动上传封面图！']);
                }

                $coverImg = $res['url'];
            }
        }


        $ndata = [
            'uid' => $input['uid'],
            'type' => $input['type'],
            'content' => $input['content'],
            'status' => 1,
            'cover_img' => $coverImg,
            'srcs' => $srcStr
        ];

        $res = UserVideos::create($ndata);
        if ($res->id) {
            Redis::select(3);
            $RedisKey = 'resources_praise';
            Redis::zAdd($RedisKey, 0, $res->id);
            return response()->json(['status' => 1, 'msg' => '发布成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '发布失败']);
        }
    }

    function getVideoCover($file, $time, $name)
    {

        if (empty($time)) $time = '1';//默认截取第一秒第一帧

        $strlen = strlen($file);

        $videoCover = substr($file, 0, $strlen - 4);

        $videoCoverName = $videoCover . '.jpg';//缩略图命名

        exec("ffmpeg -i " . $file . " -y -f mjpeg -ss " . $time . " -t 0.001 -s 320x240 " . $name . "", $out, $status);

        $str = "ffmpeg -i " . $file . " -y -f mjpeg -ss 3 -t " . $time . " -s 320x240 " . $name;

//echo $str."";

        $result = system($str);

    }

    /*
     * 关注用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addAttention(Request $request)
    {
        $input = $request->only(['uid', 'tuid']);

        $rules = [
            'tuid' => 'required',
        ];

        $messages = [
            'tuid.required' => '被关注用户id缺失',
        ];

        $validate = Validator::make($input, $rules, $messages);
        if ($validate->fails()) {
            return response()->json(['status' => 0, 'msg' => $validate->errors()->first()]);
        }

        $ainfo = UserAttentions::where('fuid', $input['uid'])
            ->where('tuid', $input['tuid'])
            ->first();

        if ($ainfo) {
            return response()->json(['status' => 0, 'msg' => '已关注']);
        }

        $ndata = [
            'fuid' => $input['uid'],
            'tuid' => $input['tuid']
        ];

        $res = UserAttentions::create($ndata);
        if ($res->id) {
            return response()->json(['status' => 1, 'msg' => '关注成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '关注失败']);
        }
    }

    /**
     * 取消关注
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delAttention(Request $request)
    {
        $input = $request->only(['uid', 'tuid']);

        $rules = [
            'tuid' => 'required',
        ];

        $messages = [
            'tuid.required' => '被关注用户id缺失',
        ];

        $validate = Validator::make($input, $rules, $messages);
        if ($validate->fails()) {
            return response()->json(['status' => 0, 'msg' => $validate->errors()->first()]);
        }

        $ainfo = UserAttentions::where('fuid', $input['uid'])
            ->where('tuid', $input['tuid'])
            ->first();
        if (!$ainfo) {
            return response()->json(['status' => 0, 'msg' => '尚未关注']);
        }

        $res = UserAttentions::destroy($ainfo->id);
        if ($res) {
            return response()->json(['status' => 1, 'msg' => '取消关注成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '取消关注失败']);
        }
    }

    /**
     * 视频资源点赞
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addPraise(Request $request)
    {
        $input = $request->only(['uid', 'video_id']);

        $rules = [
            'video_id' => 'required',
        ];

        $messages = [
            'video_id.required' => '视频id缺失',
        ];

        $validate = Validator::make($input, $rules, $messages);
        if ($validate->fails()) {
            return response()->json(['status' => 0, 'msg' => $validate->errors()->first()]);
        }

        $pinfo = UserVideoPraise::where('uid', $input['uid'])
            ->where('video_id', $input['video_id'])
            ->first();

        Redis::select(3);
        $RedisKey = 'resources_praise';
        if ($pinfo) {
            $res = UserVideoPraise::destroy($pinfo->id);
            if ($res) {
                if (Redis::exists($RedisKey)) {
                    $score = Redis::zScore($RedisKey, $pinfo->video_id);
                    if ($score && $score >= 1) {

                    }
                }
                return response()->json(['status' => 1, 'msg' => '取消点赞成功']);
            } else {
                return response()->json(['status' => 0, 'msg' => '取消点赞失败']);
            }
        }

        $ndata = [
            'uid' => $input['uid'],
            'video_id' => $input['video_id']
        ];

        $res = UserVideoPraise::create($ndata);
        if ($res->id) {
            if (!Redis::exists($RedisKey)) {
                Redis::zAdd($RedisKey, 1, $res->video_id);
            } else {
                Redis::zIncrBy($RedisKey, 1, $res->video_id);
            }
            return response()->json(['status' => 1, 'msg' => '点赞成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '点赞失败']);
        }
    }

    /**
     * 视频收藏
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCollect(Request $request)
    {
        $input = $request->only(['uid', 'video_id']);

        $rules = [
            'video_id' => 'required',
        ];

        $messages = [
            'video_id.required' => '视频id缺失',
        ];

        $validate = Validator::make($input, $rules, $messages);
        if ($validate->fails()) {
            return response()->json(['status' => 0, 'msg' => $validate->errors()->first()]);
        }

        $cinfo = UserVideoCollect::where('uid', $input['uid'])
            ->where('video_id', $input['video_id'])
            ->first();
        if ($cinfo) {
            $res = UserVideoCollect::destroy($cinfo->id);
            if ($res) {
                return response()->json(['status' => 1, 'msg' => '取消收藏成功']);
            } else {
                return response()->json(['status' => 0, 'msg' => '取消收藏失败']);
            }
        }

        $ndata = [
            'uid' => $input['uid'],
            'video_id' => $input['video_id']
        ];

        $res = UserVideoCollect::create($ndata);
        if ($res->id) {
            return response()->json(['status' => 1, 'msg' => '收藏成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '收藏失败']);
        }
    }

    /**
     * 视频列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addBroswer(Request $request)
    {
        $input = $request->only(['uid', 'video_id']);

        $rules = [
            'video_id' => 'required',
        ];

        $messages = [
            'video_id.required' => '视频id缺失',
        ];

        $validate = Validator::make($input, $rules, $messages);
        if ($validate->fails()) {
            return response()->json(['status' => 0, 'msg' => $validate->errors()->first()]);
        }

        $cinfo = UserVideoBroswer::where('uid', $input['uid'])
            ->where('video_id', $input['video_id'])
            ->first();
        if ($cinfo) {
            return response()->json(['status' => 0, 'msg' => '已添加']);
        }

        $ndata = [
            'uid' => $input['uid'],
            'video_id' => $input['video_id']
        ];

        $res = UserVideoBroswer::create($ndata);
        if ($res->id) {
            return response()->json(['status' => 1, 'msg' => '添加成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '添加失败']);
        }
    }

    /**
     * 我的视频
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myVedios(Request $request)
    {
        $uid = $request->uid;
        $tuid = $request->tuid;
        if ($tuid > 0) {
            $uid = $tuid;
        }
        $page = $request->page;
        if (!$page) {
            $page = 1;
        }
        $start = ($page - 1) * self::$totalList;

        $totalNum = UserVideos::where('uid', $uid)
            ->count();
        $videos = UserVideos::select('id', 'cover_img')
            ->where('uid', $uid)
            ->orderBy('id', 'desc')
            ->skip($start)
            ->take(self::$totalList)
            ->get();

        if ($videos) {
            foreach ($videos as $v) {
                $v->praise_num = UserVideoPraise::where('video_id', $v['id'])->count();
                $v->broswer_num = UserVideoBroswer::where('video_id', $v['id'])->count();
            }
        }

        return response()->json(['status' => 1, 'data' => ['list' => $videos, 'totalNum' => $totalNum]]);
    }

    /**
     * 删除视频
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delMyVedio(Request $request)
    {

        $uid = $request->uid;
        $id = $request->id;

        $info = UserVideos::where('uid', $uid)
            ->where('id', $id)
            ->first();
        
        if (!$info) {
            return response()->json(['status' => 0, 'msg' => '该视频不存在！']);
        }

        $res = UserVideos::where('id', $id)
            ->delete();

        if (!$res) {
            return response()->json(['status' => 0, 'msg' => '删除失败！']);
        }

        return response()->json(['status' => 1, 'msg' => '删除成功！']);
    }

    /**
     * 我的点赞视频
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myPraiseVedios(Request $request)
    {
        $uid = $request->uid;
        $index = $request->index;
        $page = $request->page;
        if (!$page) {
            $page = 1;
        }
        $start = $index + ($page - 1) * self::$totalList;

        $totalNum = DB::table('user_video_praise')
            ->join('user_videos', 'user_video_praise.video_id', '=', 'user_videos.id')
            ->where('user_video_praise.uid', $uid)
            ->orderBy('user_video_praise.id', 'desc')
            ->skip($start)
            ->take(self::$totalList)
            ->count();
        $videos = DB::table('user_video_praise')
            ->join('user_videos', 'user_video_praise.video_id', '=', 'user_videos.id')
            ->select('user_videos.*')
            ->where('user_video_praise.uid', $uid)
            ->orderBy('user_video_praise.id', 'desc')
            ->skip($start)
            ->take(self::$totalList)
            ->get();

        if ($videos) {
            foreach ($videos as $v) {
                $v->srcs = explode(',', $v->srcs);

                $table = 'resume';
                $field1 = 'avatar';
                $field2 = 'realname';

                $avatar = DB::table($table)->where('uid', $v->uid)->value($field1);
                $name = DB::table($table)->where('uid', $v->uid)->value($field2);
                $v->avatar = $avatar;
                $v->name = $name;
                $v->praise_num = UserVideoPraise::where('video_id', $v->id)->count();
                $v->collect_num = UserVideoCollect::where('video_id', $v->id)->count();

                $is_attention = UserAttentions::where('fuid', $uid)->where('tuid', $v->uid)->value('id');
                $is_praise = UserVideoPraise::where('uid', $uid)->where('video_id', $v->id)->value('id');
                $is_collect = UserVideoCollect::where('uid', $uid)->where('video_id', $v->id)->value('id');

                $v->is_attention = $is_attention > 0 ? 1 : 0;
                $v->is_praise = $is_praise > 0 ? 1 : 0;
                $v->is_collect = $is_collect > 0 ? 1 : 0;
            }
        }

        return response()->json(['status' => 1, 'data' => ['list' => $videos, 'totalNum' => $totalNum]]);
    }

    /**
     * 我的收藏视频
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myCollectVedios(Request $request)
    {
        $uid = $request->uid;
        $index = $request->index;
        $page = $request->page;
        if (!$page) {
            $page = 1;
        }
        $start = $index + ($page - 1) * self::$totalList;
        $totalNum = DB::table('user_video_collect')
            ->join('user_videos', 'user_video_collect.video_id', '=', 'user_videos.id')
            ->where('user_video_collect.uid', $uid)
            ->orderBy('user_video_collect.id', 'desc')
            ->skip($start)
            ->take(self::$totalList)
            ->count();
        $videos = DB::table('user_video_collect')
            ->join('user_videos', 'user_video_collect.video_id', '=', 'user_videos.id')
            ->select('user_videos.*')
            ->where('user_video_collect.uid', $uid)
            ->orderBy('user_video_collect.id', 'desc')
            ->skip($start)
            ->take(self::$totalList)
            ->get();

        if ($videos) {
            foreach ($videos as $v) {
                $v->srcs = explode(',', $v->srcs);

                $table = 'resume';
                $field1 = 'avatar';
                $field2 = 'realname';

                $avatar = DB::table($table)->where('uid', $v->uid)->value($field1);
                $name = DB::table($table)->where('uid', $v->uid)->value($field2);
                $v->avatar = $avatar;
                $v->name = $name;
                $v->praise_num = UserVideoPraise::where('video_id', $v->id)->count();
                $v->collect_num = UserVideoCollect::where('video_id', $v->id)->count();

                $is_attention = UserAttentions::where('fuid', $uid)->where('tuid', $v->uid)->value('id');
                $is_praise = UserVideoPraise::where('uid', $uid)->where('video_id', $v->id)->value('id');
                $is_collect = UserVideoCollect::where('uid', $uid)->where('video_id', $v->id)->value('id');

                $v->is_attention = $is_attention > 0 ? 1 : 0;
                $v->is_praise = $is_praise > 0 ? 1 : 0;
                $v->is_collect = $is_collect > 0 ? 1 : 0;
            }
        }

        return response()->json(['status' => 1, 'data' => ['list' => $videos, 'totalNum' => $totalNum]]);
    }

    /**
     * 我的粉丝
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myAttentions(Request $request)
    {
        $uid = $request->uid;
        $page = $request->page;
        if (!$page) {
            $page = 1;
        }
        $totalList = 20;
        $start = ($page - 1) * $totalList;

        $totalNum = UserAttentions::where('tuid', $uid)
            ->count();

        $attentions = UserAttentions::select('fuid')
            ->where('tuid', $uid)
            ->orderBy('id', 'desc')
            ->skip($start)
            ->take($totalList)
            ->get();

        if ($attentions) {
            foreach ($attentions as $v) {
                $info = User::select('name', 'avatar')->where('uid', $v->fuid)->first();

                $v->realname = $info->name;
                $v->avatar = $info->avatar;

                $isAttention = UserAttentions::where('fuid', $uid)
                    ->where('tuid', $v->fuid)
                    ->first();
                $v->is_attention = $isAttention ? 1 : 0;
            }
        }

        return response()->json(['status' => 1, 'data' => ['list' => $attentions, 'totalNum' => $totalNum]]);
    }

    /**
     * 我的关注
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myAttention(Request $request)
    {
        $uid = $request->uid;
        $page = $request->page;
        if (!$page) {
            $page = 1;
        }
        $totalList = 20;
        $start = ($page - 1) * $totalList;

        $totalNum = UserAttentions::where('fuid', $uid)
            ->count();

        $attentions = UserAttentions::select('tuid')
            ->where('fuid', $uid)
            ->orderBy('id', 'desc')
            ->skip($start)
            ->take($totalList)
            ->get();

        if ($attentions) {
            foreach ($attentions as $v) {

                $info = User::select('name', 'avatar')->where('uid', $v->tuid)->first();
                $v->realname = $info->name;
                $v->avatar = $info->avatar;
            }
        }

        return response()->json(['status' => 1, 'data' => ['list' => $attentions, 'totalNum' => $totalNum]]);
    }

    /**
     * 支付订单
     * @param Request $request
     * @return bool|\Illuminate\Http\JsonResponse|mixed|\SimpleXMLElement|string|\构建好的、签名后的最终跳转URL（GET）或String形式的form（POST）
     */
    public function goPayVip(Request $request)
    {
        $uid = $request->uid;
        $payType = $request->paytype;
        $levelId = $request->levelId;

        $total = Levels::where('id', $levelId)->value('price');
        if (!$total) {
            return response()->json(['status' => 0, 'msg' => '参数错误！']);
        }
        $tag = $this->create_order($uid);
        $vdata = [
            'order_number' => $tag,
            'uid' => $uid,
            'total' => $total,
            'paytype' => $payType,
            'level_id' => $levelId,
            'create_time' => time(),
            'status' => 0
        ];

        $res = VipOrder::create($vdata);

        if (!$res->id) {
            return response()->json(['status' => 0, 'msg' => '预支付失败！']);
        }

        switch ($payType) {
            case 1:
                $ip = $request->getClientIp();

                $data = [
                    "body" => '廉德会员',
                    "out_trade_no" => $tag,
                    //                "out_trade_no" => time(),
                    "client_ip" => $ip,
                    "money" => $total,
//                    "money" => 0.01,
                ];

                $type = $request->type;
                if (in_array($type, [1, 2])) {
                    $data["openid"] = $request->openid;
                }

                $payMent = new PaymentController();
                $res = $payMent->wx_pay($data, $type, 'vip');

                if ($res['status'] == 0) {
                    return response()->json(['status' => 0, 'msg' => $res['msg']]);
                } else {
                    return response()->json($res);
                }
                break;
            case 2:
                //支付宝支付
                $data = [
                    "out_trade_no" => $tag,
                    "subject" => '廉德劳务',
                    "total_amount" => $total,
//                    "total_amount" => 0.01,
                    "body" => '廉德会员',
                    "timeout_express" => '1m',
                ];

                $type = $request->type;
                if ($type == 3) {
                    $alipay = new AliPayController();
                } elseif ($type == 4) {
                    $alipay = new AliPayAppController();
                }

                $result = $alipay->ali_pay($data, 'vip');

                return $result;
                break;
            default:
                return response()->json(['status' => 0, 'message' => "请选择合法的支付方式！"]);
                break;
        }
    }

    /**
     * 举报视频
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addReport(Request $request)
    {
        $input = $request->input();
        $factory = new ValidatorFactory(new UserVideosReport());
//        Log::channel('test')->info(json_encode($input));
        $res1 = $factory->factory($input);
        if ($res1['status'] == 0) {
            return response()->json($res1);
        }

        unset($input['token']);
        $res = \App\Models\UserVideosReport::create($input);
        if ($res->id) {
            return response()->json(['status' => 1, 'msg' => '添加成功']);
        } else {
            return response()->json(['status' => 0, 'msg' => '添加失败']);
        }
    }
}
